package com.yulongtian.algorithms.doublepointer.syntropy;

/**
 * 同向双指针
 * 力扣题目：713
 * https://leetcode.cn/problems/subarray-product-less-than-k/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-09 11:20
 */
public class Test02 {
    public static void main(String[] args) {
        int[] nums = {10, 5, 2, 6};
        int k = 100;
        System.out.println(numSubarrayProductLessThanK(nums, k));
    }

    public static int numSubarrayProductLessThanK(int[] nums, int k) {
        if (k <= 1) {
            return 0;
        }
        int len = nums.length;
        //乘积
        int prod = 1;
        int left = 0;
        //结果
        int ans = 0;
        for (int right = 0; right < len; right++) {
            prod *= nums[right];
            while (prod >= k) {
                prod /= nums[left];
                left++;
            }
            //某一子数组乘积小于k，该子数组的子数组肯定也小于k
            ans += right - left + 1;
        }
        return ans;
    }
}
